package controller;

import pojo.Emp;
import service.IEmpService;
import service.impl.EmpServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;

public class EmpController1 extends HttpServlet {
    private  IEmpService ies = new EmpServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doGet(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);
    }

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //super.service(req, resp);
        String requestURI = request.getRequestURI();
        int index = requestURI.lastIndexOf("/") + 1;
        String substring = requestURI.substring(index);
        Class<EmpController1> empControllerClass = EmpController1.class;
        Method method=null;
        try {
            method=empControllerClass.getMethod(substring,HttpServletRequest.class,HttpServletResponse.class);
            method.invoke(this,request,response);
        } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
            e.printStackTrace();
        }

    }

    public void emplogin(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        response.setContentType("text/html;charset=utf-8");

        String empno = request.getParameter("empno");
        String ename = request.getParameter("ename");

        Emp login = ies.Login(new Emp(Integer.parseInt(empno), ename));

        if (login!=null){
            response.getWriter().println("<script type='text/javascript'>alert('登录成功 ok');</script>");

            ArrayList<Emp> emps=ies.queryAll();
            response.getWriter().println("<html>");
            response.getWriter().println("<body>");
            response.getWriter().println("<table>");
            response.getWriter().println("<tr><td>员工编号</td><td>员工姓名</td><td>岗位名称</td><td>工资</td><td>操作</td></tr>");

            for (Emp e:emps) {
                response.getWriter().println("<tr><td>"+e.getEmpno()+"</td><td>"+e.getEname()+"</td><td>"+e.getJob()+"</td><td>"+e.getSal()+"</td><td><a href=empdel?empno="+e.getEmpno()+">删除</a></td></tr>");
            }

            response.getWriter().println("</table>");
            response.getWriter().println("</body>");
            response.getWriter().println("</html>");

        }else {
            response.getWriter().println("<script type='text/javascript'>alert('登录失败 not ok');</script>");
        }
    }

    public void empdel(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        response.setContentType("text/html;charset=utf-8");

        String empno = request.getParameter("empno");

        boolean b = ies.removeByEmpno(Integer.parseInt(empno));

        if (b){
            response.getWriter().println("<script type='text/javascript'>alert('删除成功 ok');</script>");
        }else {
            response.getWriter().println("<script type='text/javascript'>alert('删除失败 not ok');</script>");
        }

        ArrayList<Emp> emps=ies.queryAll();
        response.getWriter().println("<html>");
        response.getWriter().println("<body>");
        response.getWriter().println("<table>");
        response.getWriter().println("<tr><td>员工编号</td><td>员工姓名</td><td>岗位名称</td><td>工资</td><td>操作</td></tr>");

        for (Emp e:emps) {
            response.getWriter().println("<tr><td>"+e.getEmpno()+"</td><td>"+e.getEname()+"</td><td>"+e.getJob()+"</td><td>"+e.getSal()+"</td><td><a href=empdel?empno="+e.getEmpno()+">删除</a></td></tr>");
        }

        response.getWriter().println("</table>");
        response.getWriter().println("</body>");
        response.getWriter().println("</html>");

    }



}
